<?php
class role
{
    private $db;
    private $table;

    public $pages;//返回分页URL

    public function __construct() {
        global $db;
        $this->db = &$db;
        $this->table = DB_PRE.'role';
    }

    /**
	 * 添加角色信息
	 *
	 * @param ARRAY() $data
	 * @return 	INT or FALSE
	 */
    public function add_role( $data )
    {
        $params = $data;
        $res = $this->db->insert( $this->table, $params );
		return $res;
    }

    /**
	 * 获取单条角色信息
	 *
	 * @param INT $roleid
	 * @return ARRAY $role
	 */
    public function get_role( $roleid )
    {
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and roleid=? LIMIT 1" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_string( $roleid );
		$role = $this->db->execute( true );
		return $role;
    }

    /**
	 * 修改角色信息
	 *
	 * @param ARRAY $data
	 * @param INT $roleid
	 * @return BOOL
	 */
    public function edit_role( $data, $roleid )
    {
        $params = $data;
		$res = $this->db->simple_update( $this->table, $params, array( 'roleid'=>$roleid ) );
		return $res;
    }

    /**
	 * 修改角色权限元信息
	 *
	 * @param STRING $data
	 * @param INT $roleid
	 * @return BOOL
	 */
    public function perm_role( $data, $roleid )
    {
        return $this->edit_role( array( 'elements'=>$data ), $roleid );
    }

    /**
	 * 删除角色信息
	 *
	 * @param INT $rid
	 * @return BOOL
	 */
    public function del_role( $rid )
    {
        return $this->edit_role( array( 'status'=>STATUS_DEL ), $rid );
    }

    /**
	 * 查询所有角色列表信息
	 *
	 * @param ARRAY() $params, STRING $order, INT $page, INT $pagesize
	 * @return 	ARRAY() $list
	 */
    public function get_list( $params, $order, $page=1, $pagesize=10 )
    {
        $where = '';
        if( is_array( $params ) && sizeof( $params ) > 0 )
        {
            $where = ' where ';
            $where .= implode( ' and ', $params );
        }
        $where .= $order;
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where );
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where . " limit ?, ?" );
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }

    /**
	 * 角色信息排序
	 *
	 * @param ARRAY() $data
	 * @return 	BOOL
	 */
    public function listorder( $data )
    {
        if( !is_array( $data ) )
        {
            return false;
        }
        foreach( $data as $roleid => $listorder )
        {
            $listorder = intval( $listorder );
            $this->edit_role( array( 'listorder' => $listorder ), $roleid );
        }
        return true;
    }

    /**
	 * 获得所有角色ID、名信息
	 *
	 * @param 空
	 * @return array $list
	 */
    public function get_all_role_list()
    {
    	$list = $this->db->query( 'SELECT roleid, rolename FROM ' . $this->table . ' WHERE status = '.STATUS_NORMAL.' order by listorder desc,inputtime desc' );
    	$array = array();
        foreach( $list as $l )
        {
            $array[$l['roleid']] = $l['rolename'];
        }
    	return $array;
    }
    
    public function get_role_by_ids($ids)
    {
    	$list = array();
    	if(!is_array($ids) || sizeof($ids) <= 0)
    	{
    		return $list;
    	}
    	$params = array_fill(0,sizeof($ids),'?');
    	$this->db->prepare('select * from '.$this->table.' where status='.STATUS_NORMAL.' and roleid in ('.implode(',',$params).')');
    	foreach ($ids as $id)
    	{
    		$this->db->set_string($id);
    	}
    	$res = $this->db->execute();
    	if(is_array($res))
    	{
    		foreach ($res as $v)
    		{
    			$list[$v['roleid']] =  $v;
    		}
    	}
    	return $list;
    }
}
?>
